#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup using test-plugin, purge the volume and run
# bscan to see if plugin objects were restored.

TestName="bscan-plugin-object-test"
JobName=PluginTest
. scripts/functions

scripts/cleanup
scripts/copy-plugin-confs

make -C $src/src/plugins/fd install install-test-plugin
if [ $? -ne 0 ]; then
    print_debug "Failed to build and install test-plugin!"
    exit 1
fi

file=encrypt-bug.jpg
rm -rf ${cwd}/tmp/*
echo "${cwd}/README" >${cwd}/tmp/file-list

$bperl -e 'add_attribute("$conf/bacula-sd.conf", "MaximumFileIndex", "48KB", "Device")'

start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File1 volume=TestVolume001
run job=TestPluginTest storage=File1 yes
wait
status client=$CLIENT
messages
@$out ${cwd}/tmp/log2.out
list objects
sql
@$out $tmp/objs_count
select count(*) from Object;

quit
END_OF_DATA

run_bacula

# See how many plugin objects were created in the first place
objs_orig=`cat tmp/objs_count | grep '|' | cut -d'|' -f2 | tail -1`

vm_objects_orig=`cat ${cwd}/tmp/log2.out | grep 'VMWare' | wc -l`
if [ ${vm_objects_orig} -ne 2 ]; then
   estat=1
   print_debug "Wrong VMWare plugin objects count found in output: ${vm_objects_orig}, expected 2"
fi

db_objects_orig=`cat ${cwd}/tmp/log2.out | grep 'PostgreSQL' | wc -l`
if [ ${db_objects_orig} -ne 4  ]; then
   estat=1
   print_debug "Wrong PostgreSQL plugin objects count found in output: ${db_objects_orig}, expected 4"
fi

# Purge volume to delete plugin objects from db
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log3.out
purge volume=TestVolume001
@$out ${cwd}/tmp/log4.out
list objects
END_OF_DATA

run_bconsole

objects_lines=`cat ${cwd}/tmp/log4.out | grep 'VMWare' | wc -l`
if [ ${objects_lines} -ne 0 ]; then
   estat=1
   print_debug "Wrong VMWare plugin objects count found in output: ${objects_lines}, expected 0"
fi

objects_lines=`cat ${cwd}/tmp/log4.out | grep 'PostgreSQL' | wc -l`
if [ ${objects_lines} -ne 0 ]; then
   estat=1
   print_debug "Wrong PostgreSQL plugin objects count found in output: ${objects_lines}, expected 0"
fi

echo "volume=TestVolume001" >tmp/bscan.bsr

bscan_libdbi

# If the database has a password pass it to bscan
if test "x${db_password}" = "x"; then
  PASSWD=
else
  PASSWD="-P ${db_password}"
fi

if test "$debug" -eq 1 ; then
  $bin/bscan -w working $BSCANLIBDBI -u ${db_user} -n ${db_name} $PASSWD -m -s -v -b tmp/bscan.bsr -c bin/bacula-sd.conf ${cwd}/tmp
else
  $bin/bscan -w working $BSCANLIBDBI -u ${db_user} -n ${db_name} $PASSWD -m -s -v -b tmp/bscan.bsr -c bin/bacula-sd.conf ${cwd}/tmp >tmp/log5.out 2>&1
fi

# Check if plugin objects restored successfully
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log6.out
list objects
END_OF_DATA

run_bconsole

objects_lines=`cat ${cwd}/tmp/log6.out | grep 'VMWare' | wc -l`
if [ ${objects_lines} -ne ${vm_objects_orig} ]; then
   estat=1
   print_debug "Wrong plugin objects count found in output: ${objects_lines}, expected ${vm_objects_orig}"
fi

objects_lines=`cat ${cwd}/tmp/log6.out | grep 'PostgreSQL' | wc -l`
if [ ${objects_lines} -ne ${db_objects_orig} ]; then
   estat=1
   print_debug "Wrong plugin objects count found in output: ${objects_lines}, expected ${db_objects_orig}"
fi

# Check if running bscan once more won't create any duplicates
if test "$debug" -eq 1 ; then
  $bin/bscan -w working $BSCANLIBDBI -u ${db_user} -n ${db_name} $PASSWD -m -s -v -b tmp/bscan.bsr -c bin/bacula-sd.conf ${cwd}/tmp
else
  $bin/bscan -w working $BSCANLIBDBI -u ${db_user} -n ${db_name} $PASSWD -m -s -v -b tmp/bscan.bsr -c bin/bacula-sd.conf ${cwd}/tmp >tmp/log6.out 2>&1
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
sql
@$out $tmp/objs_count_double_bscan
select count(*) from Object;

quit
quit
END_OF_DATA

run_bconsole

# Check if there are no duplicates
objs_after_bscan=`cat tmp/objs_count_double_bscan | grep '|' | cut -d'|' -f2 | tail -1`
if [ "${objs_after_bscan}" -ne "${objs_orig}" ]; then
    print_debug "ERROR: Running bscan twice produced some duplicates of plugin objects. \
                 Original count: ${objs_orig}, after second bscan: ${objs_after_bscan}"
    bstat=1
fi

stop_bacula
end_test
